package com.baomidou.mybatisplus.samples.tenant.aspect;

import com.baomidou.mybatisplus.samples.tenant.config.TenantContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@AllArgsConstructor
@Component
public class IgnoreTenantAspect {

    @Around("@annotation(com.baomidou.mybatisplus.samples.tenant.annonation.IgnoreTenant)")
    public Object doWithoutTenant(ProceedingJoinPoint proceedingJoinPoint) {
        try {
            // 忽略租户
            TenantContext.setIgnore(true);
            return proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            // 不忽略租户
            TenantContext.setIgnore(false);
        }
        return null;
    }
}